package org.lc.cmd.plugin.scan;

import org.lc.cmd.ResultHandler;
import org.lc.cmd.util.CmdOutUtil;
import org.lc.cmd.util.ContextUtil;
import org.lc.cmd.util.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
@Order(100)
public class CollectionHandler implements ResultHandler {
    private final Lazy<ResultManager> resultManager = new Lazy<>(() -> ContextUtil.getBean(ResultManager.class));

    @Override
    public Object handle(Object result, String line) {
        if (result instanceof Collection) {
            for (Object item : ((Collection<?>) result)) {
                resultManager.get().handleResult(item, line);
                CmdOutUtil.writeln();
            }
            return null;
        }
        return result;
    }
}
